import {UserType} from "@/models/types";
<template>
  <home-com
    :enums="enums"
    project-name="用户管理系统"
    :default-enum-active="enumActive"
    :enum-index-key="enumIndexKey"
    enum-key-key="_id"
    @enum-select="enumSelect"
  >
    <template v-slot:header-left>
      <el-button type="text" @click="projectSystem">项目系统</el-button>
    </template>
  </home-com>
</template>

<script lang="ts">
  import {Component, Vue} from 'vue-property-decorator'
  import {Getter} from 'vuex-class'
  import HomeCom from '@/components/layout/home/index.vue'
  import {EnumType} from '@/components/global/enum/types'
  import {UserType} from '@/models/types'
  import config from '@/project/admin/config'

  @Component({
    components: {
      HomeCom
    }
  })

  export default class Home extends Vue{

    @Getter('enums') enums!: Array<EnumType>
    @Getter('userType') userType?: UserType

    enumIndexKey = 'path'
    enumActive = ''

    /**
     * 获取符合权限的数据
     */
    getConformEnum(arr: Array<EnumType>): Array<EnumType> {
      return arr.filter((item) => {
        if (item.children && item.children.length) {
          item.children = this.getConformEnum(item.children)
        }
        return item.auth === this.userType
      })
    }

    /**
     * 跳转项目系统
     */
    projectSystem() {
      location.href = process.env.VUE_APP_JUMP_ROOT_URL + '/index.html'
    }

    enumSelect(path: string) {
      this.$router.push(path)
    }

    /**
     * 登陆成功后调用
     */
    mounted() {
      const enums = this.userType === UserType.SUPER_ADMIN ? config.enums : this.getConformEnum(config.enums)
      this.$store.commit('SET_ENUMS', enums)
      const { path, meta = {} } = this.$route
      this.enumActive = meta.redirectEnum || path
    }
  }
</script>

<style lang="scss">
</style>
